<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    #########################################################################################
    # Programmer : nawattapon yoskamtorn						    #
    # Email	: contact@nawattapon.com						    #
    # Website	: http://www.nawattapon.com						    #
    #########################################################################################
    
    class Setting extends CI_Controller
    {
	function __construct()
	{
	    parent::__construct();
	    if( ! $this->session->userdata('lang')){
                $this->session->set_userdata('lang','english');
            }
            
            if( ! $this->session->userdata('admin_login')){
		redirect('/admin/login/' , 'refresh');
	    }
            
	    $this->template->set_template('backend');
            $this->lang->load('admin/main', $this->session->userdata('lang'));
	    
	    /* check privilege */
	    $action = $this->uri->segment(4) ? $this->uri->segment(4) : 'view';
	    if( ! check_privilege($this->session->userdata['admin']['user_group_id'] , 'video_articles' , $action)){
		show_error( $this->lang->line('permission_denied') );
	    }
            
            $this->load->model('setting_model','setting_model');
	}
	
	public function index()
	{
	    $setting_id = array('id' => array('1','2','3','4','5','6'));
	    $this->setting_model->set_where_in($setting_id);
	    $setting_list = $this->setting_model->load_records();
	    
	    $value = json_decode($setting_list['0']['val']);
	    $data['title_en'] = $value->english;
	    $data['title_th'] = $value->thai;
	    
	    $value = json_decode($setting_list['1']['val']);
	    $data['description_en'] = $value->english;
	    $data['description_th'] = $value->thai;
	    
	    $value = json_decode($setting_list['2']['val']);
	    $data['keyword'] = $value->val;
	    
	    $value = json_decode($setting_list['3']['val']);
	    $data['facebook'] = $value->val;
	    
	    $value = json_decode($setting_list['4']['val']);
	    $data['twitter'] = $value->val;
	    
	    $value = json_decode($setting_list['5']['val']);
	    $lang = $value->val;
	    $data['lang_en'] = '';
	    $data['lang_th'] = '';
	    switch($lang){
		case 'thai' :
		    $data['lang_en'] = 'selected="selected"';
		    break;
		default :
		    $data['lang_en'] = 'selected="selected"';
	    }
	    
	    $this->template->write('title', $this->lang->line('back-end') ,FALSE);
	    $this->template->write_view('header','setting_js');
	    $this->template->write_view('sidebar','master/sidebar_view');
	    $this->template->write_view('content','setting_view',$data);
            $this->template->render();
	}
	
	public function update()
	{
            if (isset($_POST['default_language'])){
		
		$input['val'] = json_encode(array('english'=>$this->input->post('site_title_en') , 'thai'=>$this->input->post('site_title_th')));
		$this->setting_model->set_where(array('id'=>'1'));
                $this->setting_model->update_record($input);
		
		$input = array();
		$input['val'] = json_encode(array('english'=>$this->input->post('site_description_en') , 'thai'=>$this->input->post('site_description_th')));
		$this->setting_model->set_where(array('id'=>'2'));
                $this->setting_model->update_record($input);
		
		$input = array();
		$this->setting_model->set_where(array('id'=>'3'));
		$input['val'] = json_encode(array('val'=>$this->input->post('site_keyword')));
                $this->setting_model->update_record($input);
		
		$input = array();
		$this->setting_model->set_where(array('id'=>'4'));
		$input['val'] = json_encode(array('val'=>$this->input->post('facebook')));
                $this->setting_model->update_record($input);
		
		$input = array();
		$this->setting_model->set_where(array('id'=>'5'));
		$input['val'] = json_encode(array('val'=>$this->input->post('twitter')));
                $this->setting_model->update_record($input);
		
                redirect('/admin/setting/' , 'refresh');
            }
	}
    }